// 导入必要的模块
import fetch from 'node-fetch'
import { segment } from "oicq";
import plugin from '../../../lib/plugins/plugin.js'
import Cfg from '../../../lib/config/config.js'
import YAML from 'yaml'
import fs from 'node:fs'

const file = './config/config/group.yaml'
const data = YAML.parse(fs.readFileSync(file, 'utf8'))

export class Commute extends plugin {
  constructor() {
    super({
      name: '上下班',
      dsc: '控制机器人上下班',
      event: 'message',
      priority: -10,
      rule: [
        {
          reg: `^下班$`,
          fnc: 'Down',
          permission: 'master'
        },
        {
          reg: `^上班$`,
          fnc: 'work',
          permission: 'master'
        }
      ]
    })
  }

  async Down(e) {
    if (e.isGroup) {
      data[e.group_id] = {
        enable: this.name
      }
      const yaml = YAML.stringify(data)
      fs.writeFileSync(file, yaml, 'utf8')
      try {
        const response = await fetch('https://v1.hitokoto.cn')
        const yiyandata = await response.json()
        await e.reply([`花花去睡觉了，终于可以好好休息了\n${yiyandata.hitokoto}`])
      } catch (error) {
        console.error(error)
      }
    } else {
      e.reply('请在群聊中使用')
    }
  }

  async work(e) {
    if (e.isGroup) {
      data[e.group_id] = {
        enable: null
      }
      const yaml = YAML.stringify(data)
      fs.writeFileSync(file, yaml, 'utf8')
      try {
        const response = await fetch('https://v1.hitokoto.cn')
        const yiyandata = await response.json()
        await e.reply([`是有事找我吗？真拿你没办法\n${yiyandata.hitokoto}`])
      } catch (error) {
        console.error(error)
      }
    } else {
      e.reply('请在群聊中使用')
    }
  }
}

